package com.atguigu.vod.utils;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.ResultCode;
import com.atguigu.servicebase.exceptionhandler.CustomizeException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author He Wentao
 * @since 2022/9/5 22:53
 */

public class InitVodClient {

    //填入AccessKey信息
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入地域，固定的，不需要更改
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        return new DefaultAcsClient(profile);
    }

    // 获取播放地址函数
    public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client, String videoID) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoID);
        return client.getAcsResponse(request);
    }


    /**
     * 根据 AccessKey、VideoID 来获取到视频信息
     * @param accessKeyId  accessKey
     * @param accessKeySecret  accessKeySecret
     * @param videoID 视频id
     * @return map 返回参数：
     *  “url”:视频播放地址  “name”:视频名称  “RequestId”:本次请求id
     */
    public static Map<String, String> getVodPath(String accessKeyId, String accessKeySecret, String videoID) {
        // 创建链接对象
        DefaultAcsClient client;
        // 创建请求对象
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        // 创建返回参数
        Map<String,String> map = new HashMap<>();

        try {
            // 使用 AccessKey 获取到链接对象
            client = initVodClient(accessKeyId, accessKeySecret);

            // 根据视频 id ，获取到播放视频地址
            response = getPlayInfo(client, videoID);

            // 获取到返回的相关视频信息
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();


            // 获取地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                // 封装 url
                map.put("url",playInfo.getPlayURL());
            }
            // 获取视频名称
            map.put("name", response.getVideoBase().getTitle());

        } catch (Exception e) {
            throw new CustomizeException(ResultCode.ERROR.getValue(), e.getLocalizedMessage());
        }
        // 视频 id
        map.put("RequestId",response.getRequestId());

        return map;
    }

    /**
     * 根据 accessKey 获取到视频播放凭证
     *
     * @param accessKeyId     accessKey
     * @param accessKeySecret accessKeySecret
     * @param videoID         视频 id
     * @throws Exception 异常
     */
    public static String getVodAuth(String accessKeyId, String accessKeySecret, String videoID) throws Exception {
        // 获取初始化对象
        DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret);
        // 创建获取视频凭证 Request 和 response
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

        // 向 Request 设置视频 id
        request.setVideoId(videoID);

        // 调用初始化方法，获取到视频播放凭证
        response = client.getAcsResponse(request);

        // 输出获取到的凭证
        return response.getPlayAuth();

    }


    /**
     * 本地文件上传接口
     *
     * @param accessKeyId     accessKeyId
     * @param accessKeySecret accessKeySecret
     * @param title           视频标题
     * @param fileName        要上传文件的路径
     * @return 视频 id
     */
    public static String UploadVideo(String accessKeyId, String accessKeySecret, String title, String fileName) {
        UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
        /* 可指定分片上传时每个分片的大小，默认为2M字节 */
        request.setPartSize(2 * 1024 * 1024L);
        /* 可指定分片上传时的并发线程数，默认为1，(注：该配置会占用服务器CPU资源，需根据服务器情况指定）*/
        request.setTaskNum(1);
        // 开始上传视频
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadVideoResponse response = uploader.uploadVideo(request);

        return response.getVideoId();
    }

}
